﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web;
using VioletDevelopmentLaboratory.Net.Web.Component;

public partial class vboard_component_board_template_default_view_sub_rating : BoardSubPartBase
{
	private BoardThread CurrentThread;

	private void reloadCurrentThread()
	{
		this.CurrentThread = base.CurrentBoard.GetThread(BoardParameters.ThreadID);
	}



	protected void Page_Init(Object sender, EventArgs e)
	{
		this.CurrentThread = (BoardThread)this.Page.Items["CurrentThread"];
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		if(!Page.IsPostBack)
			this.Rating_Bind(false);
	}

	protected void Rating_Bind(Boolean doSelfRating)
	{
		if (this.CurrentThread.IsDeleted || this.CurrentThread.IsBlocked)
		{
			this.Visible = false;
		}
		else
		{
			BoardThreadRatingInfoCollection ratingInfoCollection = this.CurrentThread.GetRatingInfoCollection();

			RatingImage.ImageUrl = GetLocalResourceObject(String.Format("Rating_{0}", (Int32)Math.Round(this.CurrentThread.Rating, 0))).ToString();
			RatingImage.AlternateText = String.Format("{0:F1}", this.CurrentThread.Rating);

			RatingDetailLiteral.Text = String.Format(GetLocalResourceObject("Rating_TotalFormat").ToString(), this.CurrentThread.Rating, this.CurrentThread.RatingPoint, this.CurrentThread.RatingCount);

			RatingUserRepeater.DataSource = ratingInfoCollection;
			RatingUserRepeater.DataBind();

			Boolean currentUserAlreadyRating = false;
			if (ratingInfoCollection != null)
			{
				foreach (BoardThreadRatingInfo ratingInfo in ratingInfoCollection)
				{
					if (ratingInfo.UserName.ToLower() == Page.User.Identity.Name.ToLower())
					{
						currentUserAlreadyRating = true;
						break;
					}
				}
			}

			if (doSelfRating)
			{
				//자기글 평가할 수 있도록
				RatingFormPanel.Visible = !this.CurrentThread.IsBlocked && Page.User.Identity.IsAuthenticated && !this.CurrentThread.IsClosed && !currentUserAlreadyRating;
			}
			else
			{
				//자기글 평가할 수 없도록
				RatingFormPanel.Visible = !this.CurrentThread.IsBlocked && Page.User.Identity.IsAuthenticated && !this.CurrentThread.IsClosed && !currentUserAlreadyRating && (this.CurrentThread.ProviderUserKey != base.CurrentUserInfo.ProviderUserKey);
			}
		}
	}

	protected void RatingButton_Click(Object sender, EventArgs e)
	{
		Int32 point = Int32.Parse(((IButtonControl)sender).CommandArgument);

		if (this.CurrentThread.SetRating(base.CurrentUserInfo.ProviderUserKey, base.CurrentUserInfo.Name, point) == BoardThreadSetRatingResult.Success)
		{
			ScriptManager.RegisterStartupScript(Page, this.GetType(), "RatingResult",
				VUtility.GetAlertScript(VUtility.GetVWebGlobalResource("Board_Do_ThreadRating_Success")), true);

			this.reloadCurrentThread();

			this.Rating_Bind(false);
		}
		else
		{
			ScriptManager.RegisterStartupScript(Page, this.GetType(), "RatingResult",
				VUtility.GetAlertScript(VUtility.GetVWebGlobalResource("Board_Do_ThreadRating_Failure")), true);
		}
	}
}
